#include "MobileCfmVerifyDlg.h"

#include <core/ImageMgr.h>
#include <core/StyleMgr.h>
#include <uam/UserAccountManager.h>
#include <widgets/SLineEdit.h>

#include <QLabel>
#include <QPushButton>
#include <QSpacerItem>
#include <QStylePainter>
#include <QTimer>
#include <QTimerEvent>
#include <QVBoxLayout>
#include <QWidget>
#include <regex>

MobileCfmVerifyDlg::MobileCfmVerifyDlg(QWidget* p /*= nullptr*/) : QWidget(p) {
  QVBoxLayout* vLayout = new QVBoxLayout(this);
  vLayout->setContentsMargins(81, 0, 0, 0);

  QLabel* MobileCfm = new QLabel(tr("欢迎进行手机验证:"), this);
  vLayout->addWidget(MobileCfm, Qt::AlignLeft);
  vLayout->addSpacing(5);

  QHBoxLayout* hLayout = new QHBoxLayout;
  hLayout->setContentsMargins(0, 0, 0, 0);
  QLabel* account = new QLabel(tr("您的账户:"), this);
  account->setStyleSheet(
      QString("color:#%1")
          .arg(StyleMgr::instance().color("SupportingWord").rgb(), 0, 16));
  hLayout->addWidget(account);
  hLayout->addSpacing(23);

  const CUserAccountInfo& usrInfo =
      SSUserAccountManager::instance().GetUserInfo();
  m_account = new SLineEdit(usrInfo.m_UserAccount, this);
  m_account->setFixedSize(170, 22);
  m_account->setEnabled(false);
  hLayout->addWidget(m_account);
  vLayout->addLayout(hLayout);
  vLayout->addSpacing(3);
  QHBoxLayout* hSecondLayout = new QHBoxLayout;
  QLabel* pwd = new QLabel(tr("密码:"), this);
  pwd->setStyleSheet(
      QString("color:#%1")
          .arg(StyleMgr::instance().color("SupportingWord").rgb(), 0, 16));
  m_pwd = new SLineEdit("", this);
  m_pwd->setFixedSize(170, 22);
  m_pwd->setEchoMode(QLineEdit::Password);
  hSecondLayout->addWidget(pwd);
  hSecondLayout->addSpacing(23);
  hSecondLayout->addWidget(m_pwd);
  vLayout->addLayout(hSecondLayout);
  vLayout->addSpacing(3);
  QHBoxLayout* hThirdLayout = new QHBoxLayout;
  QLabel* phoneNumber = new QLabel(tr("手机号码:"), this);
  phoneNumber->setStyleSheet(
      QString("color:#%1")
          .arg(StyleMgr::instance().color("SupportingWord").rgb(), 0, 16));
  const CUserMobileConfirmInfo& info =
      SSUserAccountManager::instance().GetMobileConfirm();
  m_phoneNumber = new SLineEdit(info.m_mobile, this);
  m_phoneNumber->setFixedSize(170, 22);
  hThirdLayout->addWidget(phoneNumber);
  hThirdLayout->addSpacing(23);
  hThirdLayout->addWidget(m_phoneNumber);
  vLayout->addLayout(hThirdLayout);
  vLayout->addSpacing(3);

  QHBoxLayout* hFourthLayout = new QHBoxLayout;
  QLabel* verifyCode = new QLabel(tr("验证码:"), this);
  verifyCode->setStyleSheet(
      QString("color:#%1")
          .arg(StyleMgr::instance().color("SupportingWord").rgb(), 0, 16));
  hFourthLayout->addWidget(verifyCode);
  hFourthLayout->addSpacing(39);
  m_codeVerify = new SLineEdit("", this);
  m_codeVerify->setFixedSize(70, 22);
  hFourthLayout->addWidget(m_codeVerify);
  hFourthLayout->addSpacing(2);
  m_getCode = new QPushButton(tr("获取验证码"), this);
  m_getCode->setFixedSize(95, 22);

  QString style =
      QString(
          "QPushButton{background:#%1; "
          "border-radius:0px;color:#FFE5C7}\nQPushButton:hover{background:#%2}"
          "\n"
          "QPushButton:disabled{background-color:#%3;color:#%4;}})")
          .arg(StyleMgr::instance().color("ButtonNormal").rgb(), 0, 16)
          .arg(StyleMgr::instance().color("Hover").rgb(), 0, 16)
          .arg(StyleMgr::instance().color("ButtonDisable").rgb(), 0, 16)
          .arg(StyleMgr::instance().color("TxtDisabled").rgb(), 0, 16);

  m_getCode->setStyleSheet(style);
  hFourthLayout->addWidget(m_getCode);
  vLayout->addLayout(hFourthLayout);

  QHBoxLayout* hFifthLayout = new QHBoxLayout;
  hFifthLayout->setContentsMargins(0, 99, 4, 0);
  m_quickVerify = new QPushButton(tr("立即验证"), this);
  m_quickVerify->setFixedSize(100, 30);
  m_quickVerify->setStyleSheet(style);

  hFifthLayout->addWidget(m_quickVerify);
  vLayout->addLayout(hFifthLayout);

  QHBoxLayout* hSixthLayout = new QHBoxLayout;
  hSixthLayout->setContentsMargins(100, 20, 0, 0);
  m_nextTime = new SMobileVerifyLabel(tr("下次再说"), this);
  hSixthLayout->addWidget(m_nextTime);
  vLayout->addLayout(hSixthLayout);
}

void MobileCfmVerifyDlg::paintEvent(QPaintEvent* event) {
  QWidget::paintEvent(event);
  QStylePainter painter(this);
  painter.setRenderHint(QPainter::Antialiasing);
  // painter.setPen(StyleMgr::instance().color("Up_Increase_H1"));
  painter.setPen(QColor(213, 34, 34));
  QRect starR;
  QString strStar = "*";
  QFontMetrics fm(font());

  int startX = rect().x() + 70;
  int starWidth = fm.horizontalAdvance(strStar);
  for (int i = 0; i < 3; i++) {
    starR.setRect(startX, rect().y() + 62 + i * 30, starWidth, rect().height());
    painter.drawText(starR, strStar, QTextOption(Qt::AlignLeft));
  }
}
